<template>
  <div class="xminds-list-grid" :class="{ trash: type === 'trash' }">
    <el-skeleton :loading="skeletonLoading" animated>
      <template #template>
        <div class="card-skeleton-title"><el-skeleton-item /></div>
        <div class="card-skeleton"><el-skeleton-item v-for="item in 1" :key="item" /></div>
        <div class="card-skeleton-title"><el-skeleton-item /></div>
        <div class="card-skeleton"><el-skeleton-item v-for="item in 5" :key="item" /></div>
      </template>
      <template #default>
        <div class="column-header" v-if="folderList.length || mindList.length">
          <p class="item-header-column">名称</p>
          <p class="item-header-column" v-if="type === 'work'">大小</p>
          <p class="item-header-column">{{ type === 'work' ? '修改时间' : '删除时间' }}</p>
        </div>
        <div class="box-modal" v-if="showFolder && folderList.length">
          <div class="title">文件夹</div>
          <div class="list">
            <div class="folder" v-for="item in folderList" :key="item.id" @click="handleFolderClick(item)">
              <div class="information">
                <div class="logo"><img src="@/assets/images/home/folder.png" alt=""></div>
                <div class="desc">
                  <p class="name">{{ item.name }}</p>
                  <p class="tip">{{ item.childFolderCount + item.childMindCount }} 项目</p>
                </div>
              </div>
              <div class="size" v-if="type === 'work'">
                <p>-</p>
              </div>
              <div class="update-time">
                <p>{{ item[type === 'work' ? 'updateTime' : 'deleteTime'] }}</p>
              </div>
              <el-popover
                :width="200"
                :show-arrow="false"
                :teleported="false"
                :hide-after="300"
                :show-after="200"
                placement="bottom-start">
                <ul>
                  <li
                    v-for="itemName in FOLDER_OPERATE_MENUS"
                    :key="itemName.command"
                    :class="{ danger: itemName.command === 'delete' }"
                    @click.stop="handleOperateFolder(itemName.command, item)">
                    <i :class="itemName.icon"></i>
                    <span>{{ itemName.name }}</span>
                  </li>
                </ul>
                <template #reference>
                  <i class="more ri-more-line" @click.stop></i>
                </template>
              </el-popover>
            </div>
          </div>
        </div>
        <div class="box-modal" v-if="showMind && mindList.length">
          <div class="title">导图</div>
          <div class="list">
            <div class="folder" v-for="item in mindList" :key="item.id" @click="handleMindClick(item)">
              <div class="information">
                <div class="logo"><img src="@/assets/images/home/mind.png" alt=""></div>
                <div class="desc">
                  <p class="name">{{ item.name }}</p>
                </div>
                <div class="collect-tag" v-if="type === 'work'" :class="{ active: item.isCollected === 1 }">
                  <i class="ri-star-fill" v-if="item.isCollected === 1" @click.stop="updateMindCollectState(item.id, false)"></i>
                  <i class="ri-star-line" v-else @click.stop="updateMindCollectState(item.id, true)"></i>
                </div>
              </div>
              <div class="size" v-if="type === 'work'">
                <p>{{ (item.size / 1000).toFixed(1) }} kb</p>
              </div>
              <div class="update-time">
                <p>{{ item[type === 'work' ? 'updateTime' : 'deleteTime'] }}</p>
              </div>
              <el-popover
                :width="200"
                :show-arrow="false"
                :teleported="false"
                :hide-after="300"
                :show-after="200"
                placement="bottom-start">
                <ul>
                  <li
                    v-for="itemName in MIND_OPERATE_MENUS"
                    :class="{ danger: itemName.command === 'delete' }"
                    :key="itemName.command"
                    @click.stop="handleOperateMind(itemName.command, item)">
                    <i :class="itemName.icon"></i>
                    <span>{{ itemName.name }}</span>
                  </li>
                </ul>
                <template #reference>
                  <i class="more ri-more-line" @click.stop></i>
                </template>
              </el-popover>
            </div>
          </div>
        </div>
        <div class="empty-mind-text" v-if="!folderList.length && !mindList.length">未找到Gmind文件</div>
      </template>
    </el-skeleton>

    <NewFolderMindModal
      v-if="folderRenameModalVisible"
      :folderDetail="folderDetail"
      :mindDetail="mindDetail"
      :operateType="operateType"
      @close-modal="handleCloseModal" />

    <MoveToFolder
      v-if="moveToFolderModalVisible"
      :folderDetail="folderDetail"
      :mindDetail="mindDetail"
      :operateType="operateType"
      @close-modal="handleCloseModal" />

    <ShareMindModal v-if="shareModalVisible" :mindId="mindDetail?.id" @close-modal="handleCloseModal" />
  </div>
</template>

<script setup lang="ts">
import { defineProps, PropType, defineEmits } from 'vue'
import useViewCardList from '@/hooks/useViewCardList'
import type { Folder, Mind } from '@/types'

const props = defineProps({
  skeletonLoading: Boolean,
  showFolder: {
    type: Boolean,
    default: false
  },
  showMind: {
    type: Boolean,
    default: true
  },
  folderList: {
    type: Array as PropType<Folder[]>,
    default: () => []
  },
  mindList: {
    type: Array as PropType<Mind[]>,
    default: () => []
  },
  type: {
    type: String as PropType<'work' | 'trash'>,
    default: 'work'
  }
})

const emits = defineEmits(['refresh-page', 'update-collect-status', 'folder-click', 'mind-click'])

const {
  FOLDER_OPERATE_MENUS,
  MIND_OPERATE_MENUS,
  folderRenameModalVisible,
  shareModalVisible,
  moveToFolderModalVisible,
  folderDetail,
  mindDetail,
  operateType,
  handleFolderClick,
  handleMindClick,
  handleOperateFolder,
  handleOperateMind,
  handleCloseModal,
  updateMindCollectState,
  MoveToFolder,
  ShareMindModal,
  NewFolderMindModal
} = useViewCardList(emits, props.type)
</script>

<style lang="scss" scoped>
.xminds-list-grid {
  padding: 8px 2px;
  min-height: 360px;

  .card-skeleton-title {
    margin-top: 36px;
    .el-skeleton__item  {
      width: 160px;
      height: 36px;
    }
  }

  .card-skeleton {
    margin-top: 20px;
    gap: 16px;
    display: grid;
    .el-skeleton__item {
      height: auto;
      height: 42px;
    }
  }

  &.trash {
    .column-header {
      grid-template-columns: minmax(200px, 5fr) minmax(70px, 132px) 48px;
    }
    .box-modal {
      .list {
        .folder {
          grid-template-columns: minmax(200px, 5fr) minmax(70px, 132px) 48px;
        }
      }
    }
  }
  .column-header {
    display: grid;
    grid-template-columns: minmax(200px, 5fr) minmax(80px, 2fr) minmax(70px, 132px) 48px;
    grid-column-gap: 48px;
    padding: 0 12px;
    margin-top: 24px;
    .item-header-column {
      margin-right: 4px;
      user-select: none;
      border-radius: 4px;
      font-size: 13px;
      line-height: 18px;
      color: #949699;
    }
  }
  .box-modal {
    margin-top: 36px;
    .title {
      font-size: 12px;
      line-height: 18px;
      color: #949699;
      margin: 12px 0;
      padding: 0 12px;
    }
    .list {
      .folder {
        display: grid;
        grid-template-columns: minmax(200px, 5fr) minmax(80px, 2fr) minmax(70px, 132px) 48px;
        grid-column-gap: 48px;
        align-items: center;
        padding: 12px;
        border-radius: 8px;
        position: relative;
        & + .folder {
          margin-top: 8px;
        }
        .information {
          display: flex;
          align-items: center;
          .logo img {
            width: 32px;
            display: block;
          }
          .desc {
            margin-left: 8px;
            .name {
              font-size: 13px;
              color: #202020;
              font-weight: 600;
            }
            .tip {
              font-size: 12px;
              color: #999;
              margin-top: 6px;
            }
          }
        }
        .collect-tag {
          margin-left: auto;
          color: #999;
          font-size: 20px;
          display: none;
          cursor: pointer;
          &.active {
            display: block;
            i {
              color: var(--el-color-primary);
            }
          }
        }
        ul {
          li {
            padding: 8px 6px;
            border-radius: 6px;
            cursor: pointer;
            display: flex;
            align-items: center;
            color: #333;
            &.danger {
              color: #b10a0a;
            }
            & + li {
              margin-top: 4px;
            }
            &:hover {
              background-color: #e1e1e190;
            }
            i {
              font-size: 18px;
              position: relative;
              top: 1px;
            }
            span {
              font-size: 14px;
              margin-left: 6px;
            }
          }
        }
        &:hover {
          background-color: rgba(0,0,0,0.03);
          .collect-tag, .more {
            display: block;
          }
        }
        .size, .update-time {
          font-size: 13px;
          color: #202020;
        }
        .more {
          display: none;
          color: #999;
          background-color: rgba(0, 0, 0, .05);
          border-radius: 4px;
          padding: 0 4px;
          position: absolute;
          right: 12px;
          top: 20px;
        }
      }
    }
  }
  .empty-mind-text {
    text-align: center;
    font-size: 15px;
    color: #999;
    margin-top: 72px;
  }
}
</style>
